package com.study.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.study.book.common.AuthenticationRequired;
import com.study.book.common.R;
import com.study.book.dto.LibrarySeatDetailDto;
import com.study.book.mapper.AdminMapper;
import com.study.book.mapper.LibrarySeatDetailMapper;
import com.study.book.pojo.*;
import com.study.book.service.LibraryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "图书馆接口")
@RestController
@RequestMapping("/app/library")
public class LibraryController {

    @Autowired
    private LibraryService libraryService;

    @Autowired
    private LibrarySeatDetailMapper librarySeatDetailMapper;

    @Autowired
    private AdminMapper adminMapper;


    @ApiOperation("获取总的推荐的图书馆列表，从推荐数从高到低排序")
    @GetMapping("/getRecommendLibraryList")
    public R<List<Library>> getRecommendLibraryList(){
        List<Library> libraryList = libraryService.getRecommendLibraryList();
        return R.ok(libraryList);
    }

    @ApiOperation("获取选择地区推荐的图书馆列表")
    @GetMapping("/getAreaLibraryList/{area}")
    public R<List<Library>> getAreaLibraryList(@PathVariable("area") String area){
        List<Library> libraryList = libraryService.getAreaLibraryList(area);
        return R.ok(libraryList);
    }

    @ApiOperation("获取图书馆详细信息")
    @GetMapping("/getAreaLibraryById/{libraryId}")
    public R<Library> getAreaLibraryById(@PathVariable("libraryId") String libraryId){
        return R.ok(libraryService.getById(libraryId));
    }

    @ApiOperation("通过图书馆id获取预约订单的信息，其中包括了评论信息")
    @GetMapping("/getComment/{libraryId}")
    public R<List<LibrarySeatDetail>> getComment(@PathVariable("libraryId") Long libraryId){
        LambdaQueryWrapper<LibrarySeatDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LibrarySeatDetail::getLibraryid,libraryId);
        List<LibrarySeatDetail> reserveList = librarySeatDetailMapper.selectList(lambdaQueryWrapper);
        reserveList.replaceAll(e->{e.setAdminName(adminMapper.selectById(e.getUserId()).getUsername());return e;});
        return R.ok(reserveList);
    }

    @ApiOperation("搜索图书馆")
    @GetMapping("/searchLibrary/{word}")
    public R<List<Library>> searchLibrary(@PathVariable("word") String word){
        LambdaQueryWrapper<Library> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Library::getLibraryname,word);
        return R.ok(libraryService.list(lambdaQueryWrapper));
    }

    @ApiOperation("搜索图书馆")
    @GetMapping("/searchLibrarys/{area}/{word}")
    public R<List<Library>> searchLibrary02(@PathVariable("area") String area,@PathVariable("word") String word){
        LambdaQueryWrapper<Library> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!area.equals("0"))lambdaQueryWrapper.eq(Library::getArea,area);
        if (!word.equals("0"))lambdaQueryWrapper.like(Library::getLibraryname,word);
        return R.ok(libraryService.list(lambdaQueryWrapper));
    }


    @ApiOperation("获取选座中的数据")
    @PostMapping("/searchSeat")
    public R searchSeat(@RequestBody LibrarySeatDetailDto dto){
        return libraryService.searchSeat(dto);
    }

    @ApiOperation("选座位")
    @PostMapping("/creantSeatZZ")
    @AuthenticationRequired(value = "0")
    public R creantSeatZZ(HttpServletRequest request,@RequestBody LibrarySeatDetailDto dto){
        String id = (String) request.getAttribute("userId");
        Integer userId = Integer.valueOf(id);
        dto.setUserId(userId);
        return libraryService.creantSeatZZ(dto);
    }
}
